const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

const readFile = promisify(fs.readFile)   // return Promise
const writeFile = promisify(fs.writeFile)
const dbPath = path.join(__dirname, '../db.json')

exports.getDb = async () => {
  const data = await readFile(dbPath, 'UTF-8')
  return JSON.parse(data)
}

exports.saveDb = async content => {
  const data = JSON.stringify(content, null, "    ")
  await writeFile(dbPath, data)
}